package Com.HupochuanAdmin.Dal.Tab;

import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import Com.Hupochuan.Dal.DBManager.DBManager;
import Com.Hupochuan.Vo.Director;

public class DirectorTab {
	/*
	 * 本类私有对象
	 */
	private DBManager<Director> dbManager = new DBManager<Director>(Director.class);
	
	/*
	 * 单一实例
	 */
	private DirectorTab(){}
	private static DirectorTab directorTab = new DirectorTab();
	public static DirectorTab getObject(){
		return directorTab;
	}
	
	/*
	 * 添加数据
	 */
	public int appendData(String name){
		//参数部分
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" insert into Director set ");
		sqlBuff.append(" 	name=${name}, ");
		sqlBuff.append(" 	createTime=now(), ");
		sqlBuff.append(" 	updateTime=now() ");
		
		//sql参数部分
		paraHash.put("name",name);
		
		//执行sql 并返回参数
		return dbManager.updateSql(sqlBuff.toString(),paraHash);
	}
	
	/*
	 * 获得导演列表
	 */
	public List<Director> findDirectorList(){
		//参数部分
		List<Director> directorList = new Vector<Director>();
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select Director.*,( ");
		sqlBuff.append(" 	select count(id) from Movie ");
		sqlBuff.append(" 	where Movie.directorId=Director.id ");
		sqlBuff.append(" )as movieCount ");
		sqlBuff.append(" from Director ");
		sqlBuff.append(" order by weight desc,id ");
		
		//执行sql 
		directorList = this.dbManager.querySqlReturnVoList(sqlBuff.toString(), paraHash);
		
		//返回列表
		return directorList;
	}
	
	/*
	 * 获得导演ID
	 */
	public long findDirectorId(String name){
		//参数部分
		long directorId = 0;
		StringBuffer sqlBuff = new StringBuffer();
		Hashtable<String,Object> paraHash = new Hashtable<String,Object>();
		
		//sql语句部分
		sqlBuff.append(" select id from Director where name=${name} ");
		
		//sql参数部分
		paraHash.put("name",name);

		//执行sql
		directorId = dbManager.querySqlReturnLong(sqlBuff.toString(), paraHash);
		
		//返回
		return directorId;
	}
}
